package demo8;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/*
 * @Configuration
 * 告诉Spring这是一个配置类，用来代替配置文件
 * @ComponentScan("demo8")
 * 扫描制定包下的注解，并创建对象放入容器中
 */
@Configuration
@ComponentScan("demo8")
public class Config {
    /*
    @Bean注解可以向容器主动注册对象
    方法名叫什么都可以，只要添加该注解,那么返回的对象就会被存入Spring容器中
     */
    @Bean
    public Student initStudent() {
        return new Student();
    }
    @Bean
    public DruidDataSource initDataSource() {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setDriverClassName("com.mysql.jdbc.Driver");
        druidDataSource.setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8");
        druidDataSource.setUsername("root");
        druidDataSource.setPassword("root");
        return druidDataSource;
    }
}
